/**
 * 
 */
package com.scpii.universal.service.mobile;

import java.util.Date;

import org.apache.log4j.Logger;

import com.scpii.universal.hibernate.entity.ClientUserTab;
import com.scpii.universal.pb.PbUniversal.PBRESULT;
import com.scpii.universal.pb.PbUniversal.PbAddUserCS;
import com.scpii.universal.pb.PbUniversal.PbAddUserSC;
import com.scpii.universal.pb.PbUniversal.PbCheckUserCS;
import com.scpii.universal.pb.PbUniversal.PbCheckUserSC;
import com.scpii.universal.pb.PbUniversal.PbUpdateUserCheckCS;
import com.scpii.universal.pb.PbUniversal.PbUpdateUserCheckSC;
import com.scpii.universal.pb.PbUniversalBasic.PbClientUser;
import com.scpii.universal.service.ClientUserTabService;
import com.scpii.universal.util.Const;
import com.scpii.universal.util.DateAndString;

/**
 * UniversalApi
 * @author 甘建新
 * 2012-7-11下午11:03:42
 */
public class MobileUserService {
//	private ClientUserTabService clientUserTabService = new ClientUserTabService();
	private ClientUserTabService clientUserTabService;
	
	private Logger log = Logger.getLogger(MobileUserService.class);
	private String LOG_METHOD_NAME="";
	/**
	 * 验证用户信息
	 * @param checkCS
	 * @return
	 * @author 甘建新
	 * 2012-7-11下午11:06:34
	 */
	public byte[] checkUserSc(byte[] bytes){
		LOG_METHOD_NAME="checkUserSc";
		
		PbCheckUserSC.Builder pbCheckUserSCB = PbCheckUserSC.newBuilder();
		if (bytes!=null) {
			try {
				//转换参数
				PbCheckUserCS pbCheckUserCS = PbCheckUserCS.parseFrom(bytes);
				int appId = pbCheckUserCS.getIntAppId();
				String userName = pbCheckUserCS.getVarUserName();
				String userPassword = pbCheckUserCS.getVarUserPassword();
				
				log.info(LOG_METHOD_NAME+"【appId:"+appId+",userName:"+userName+",userPassword:"+userPassword+"】");
				
				ClientUserTab clientUser = clientUserTabService.clientUserLogingCheck(userName, userPassword);
				if (clientUser!=null) {
					pbCheckUserSCB.setIntUserId(clientUser.getIntUserId().intValue());
					pbCheckUserSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					pbCheckUserSCB.setIntUserId(0);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbCheckUserSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbCheckUserSCB.setIntUserId(0);
				pbCheckUserSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbCheckUserSCB.setIntUserId(0);
			pbCheckUserSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbCheckUserSCB.build().toByteArray();
	}
	
	/**
	 * 用户信息注册
	 * @param addUserCs
	 * @return
	 * @author 甘建新
	 * 2012-7-11下午11:06:48
	 */
	public byte[] addUserSc(byte[] bytes){
		LOG_METHOD_NAME="addUserSc";
		
		PbAddUserSC.Builder pbAddUserSCB = PbAddUserSC.newBuilder();
		if (bytes!=null) {
			try {
				//转换参数
				PbAddUserCS pbAddUserCS = PbAddUserCS.parseFrom(bytes);
				PbClientUser pbUser = pbAddUserCS.getPbClientUser();
				
				log.info(LOG_METHOD_NAME+"【PbAddUserCS:"+pbAddUserCS.toString()+"】");
				
				long num = clientUserTabService.clientUserNum(pbUser.getVarUserLoginName(),pbUser.getVarUserLoginPassword(), pbUser.getVarUserPhoneNum());
				if (num==0) {
					//参数设置到实体对象中
					ClientUserTab clientUser = new ClientUserTab();
					clientUser.setIntDeleteSign(0);
					clientUser.setIntAppId(pbUser.getIntAppId());
					clientUser.setIntUserSex(pbUser.getIntUserSex());
					clientUser.setIntUserId((long) pbUser.getIntUserId());
					clientUser.setIntOsType(pbUser.getIntOsType());
					
					clientUser.setVarDeviceId(pbUser.getVarDeviceId());
					clientUser.setVarImsi(pbUser.getVarDeviceId());
					clientUser.setVarNickName(pbUser.getVarUserNickName());
					clientUser.setVarUserLoginName(pbUser.getVarUserLoginName());
					clientUser.setVarUserLoginPassword(pbUser.getVarUserLoginPassword());
					clientUser.setVarUserName(pbUser.getVarUsreName());
					clientUser.setVarUserPhonenum(pbUser.getVarUserPhoneNum());
					clientUser.setVarUserWeiboAccount(pbUser.getVarUserWeiboAccount());
					
					clientUser.setDatUpdateDatetime(DateAndString.dateToString(new Date()));
					clientUser.setDatUserBirthday(pbUser.getDatUserBirthday());
					
					clientUserTabService.save(clientUser);
					pbAddUserSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					pbAddUserSCB.setExeResult(PBRESULT.OTHER);
					log.info(LOG_METHOD_NAME+":用户注册，电话号码验证失败。");
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbAddUserSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbAddUserSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbAddUserSCB.build().toByteArray();
	}
	
	/**
	 * 用户修改信息的验证
	 * @param bytes
	 * @return
	 * @author 甘建新
	 * 2012-7-24上午11:25:17
	 */
	public byte[] updateUserInfoCheck(byte[] bytes){
		LOG_METHOD_NAME="updateUserInfoCheck";
		
		PbUpdateUserCheckSC.Builder pbUpdateUserCheckSCB =PbUpdateUserCheckSC.newBuilder();
		if (bytes!=null) {
			try {
				PbUpdateUserCheckCS pbUpdateCheckCS = PbUpdateUserCheckCS.parseFrom(bytes);
				
				log.info(LOG_METHOD_NAME+"【PbUpdateUserCheckCS:"+pbUpdateCheckCS.toString()+"】");
				
				String phoneNum = pbUpdateCheckCS.getVarUserPhonenum();//手机号码
				String userName = pbUpdateCheckCS.getVarUserName();//用户名
				String varDeviceId = pbUpdateCheckCS.getVarDeviceId();//客户端设备ID
				String varImsi = pbUpdateCheckCS.getVarImsi();//客户端IMSI号码
				int osType = pbUpdateCheckCS.getOsType();//客户端系统类型
				int appId = pbUpdateCheckCS.getIntAppId();//客户端应用ID
				
				if ((phoneNum!=null && phoneNum.trim().length()>0) && (userName!=null && userName.trim().length()>0)) {
					ClientUserTab userTab = clientUserTabService.checkUserInfo(userName, phoneNum, varDeviceId, varImsi, osType, appId);
					pbUpdateUserCheckSCB.setIntUserId(userTab.getIntUserId().intValue());
					pbUpdateUserCheckSCB.setExeResult(PBRESULT.SUCCESS);
					
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					log.info(LOG_METHOD_NAME+":用户修改，信息条件验证失败。");
					pbUpdateUserCheckSCB.setExeResult(PBRESULT.FAILED);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbUpdateUserCheckSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbUpdateUserCheckSCB.setExeResult(PBRESULT.OTHER);
		}
		return pbUpdateUserCheckSCB.build().toByteArray();
	}
	
	/**
	 * 用户修改登录密码
	 * @param bytes
	 * @return
	 * @author 甘建新
	 * 2012-7-24上午11:26:03
	 */
	public byte[] updateUserPassword(byte[] bytes){
		return null;
	}

	// Setter
	public void setClientUserTabService(ClientUserTabService clientUserTabService) {
		this.clientUserTabService = clientUserTabService;
	}
}
